package com.ep.commonrecycleviewadapter;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ep.commonrecycleviewadapter.base.MultiItemTypeAdapter;
import com.ep.commonrecycleviewadapter.base.divider.DividerItemDecoration;
import com.ep.commonrecycleviewadapter.base.wrapper.EmptyWrapper;
import com.ep.commonrecycleviewadapter.base.wrapper.HeaderAndFooterWrapper;
import com.ep.commonrecycleviewadapter.base.wrapper.LoadmoreWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/1.
 */

public class SingleActivity extends AppCompatActivity {
    private static final String TAG = "SingleActivity";
    private RecyclerView mRecyclerView;
    private List<String> mDatas;

    private TestSingleAdapter mAdapter;

    private HeaderAndFooterWrapper mHeaderAndFooterWrapper;
    private EmptyWrapper mEmptyWrapper;
    private LoadmoreWrapper mLoadmoreWrapper;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);


   //     initData();

        mAdapter = new TestSingleAdapter(this, R.layout.adapter_single_item);
        mAdapter.setDatas(mDatas);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        //   mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));


        initEmptyView();
        initHeaderAndFooter();
        initLoadMore();

        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(mLoadmoreWrapper);


        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                Log.d(TAG, "onItemClick: ----->"+"onItemClick"+position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                Log.d(TAG, "onItemClick: ----->"+"onItemLongClick"+position);
                return false;
            }
        });
    }



    private void initData() {
        mDatas=new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            mDatas.add("this is a test" + i);
        }
    }

    /**
     * 初始化头部底部
     */
    private void initHeaderAndFooter()
    {
        mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mEmptyWrapper);

        TextView t1 = new TextView(this);
        t1.setText("this is header");
        TextView t2 = new TextView(this);
        t2.setText("this is foot ");
        mHeaderAndFooterWrapper.addHeaderView(t1);
        mHeaderAndFooterWrapper.addFootView(t2);
    }

    /**
     * 无数据时显示
     */
    private void initEmptyView()
    {
        mEmptyWrapper = new EmptyWrapper(mAdapter);
        mEmptyWrapper.setEmptyView(LayoutInflater.from(this).inflate(R.layout.adapter_empty_item, mRecyclerView, false));
    }

    /**
     *加载更多
     */
    private void initLoadMore() {
        mLoadmoreWrapper=new LoadmoreWrapper(mHeaderAndFooterWrapper);
        mLoadmoreWrapper.setLoadMoreView(R.layout.default_loading);
        mLoadmoreWrapper.setOnLoadMoreListener(new LoadmoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 10; i++) {
                            if (mDatas!=null){
                                 mDatas.add("add more "+i);
                            }
                        }
                        mLoadmoreWrapper.notifyDataSetChanged();
                    }
                }, 3000);

            }
        });
    }
}
